package com.koron.device.controller;

import com.koron.bean.app.AppBean;
import com.koron.bean.base.Response;
import com.koron.device.mapper.census.UserOrgMapper;
import com.koron.device.service.CensusService;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/census")
@RestController
@Api(tags = "设备报表")
public class CensusController {

    @Autowired
    private CensusService censusservice;

    @GetMapping("/eqmsgreport")
    @ApiOperation(value = "冗余设备各层级数量")
    public Response eqmsgreport() {
        eqmsgreport();
        return Response.ok();
    }

    private void eqmsgreportNB() {
        log.info("供水设备冗余设备各层级数量开始");
        List<AppBean> applist = ADOConnection.runTask(factory -> {
            return factory.getMapper(UserOrgMapper.class).queryApp();
        }, List.class);

        // 遍历所有租户
        for (AppBean appBean : applist) {
            ADOConnection.runTask(appBean.getMark(), censusservice, "eqmsgreport", Void.class, appBean.getMark(), appBean.getId());
        }
        log.info("供水设备冗余设备各层级数量结束");
    }


    @GetMapping("/eqpicturereport")
    @ApiOperation(value = "冗余设备图片数量")
    public Response eqpicturereport() {
        eqpicturereportNB();
        return Response.ok();
    }

    private void eqpicturereportNB() {
        log.info("供水设备冗余设备图片数量启动开始");
        List<AppBean> applist = ADOConnection.runTask(factory -> {
            return factory.getMapper(UserOrgMapper.class).queryApp();
        }, List.class);

        // 遍历所有租户
        for (AppBean appBean : applist) {
            ADOConnection.runTask(appBean.getMark(), censusservice, "eqpicturereport", Void.class, appBean.getMark(), appBean.getId());
        }
        log.info("供水设备冗余设备图片数量结束");
    }


    //    @ApiOperation(value = "供水设备信息同步任务-定时任务")// 不可以写swagger 日志切面会拿不到 request 导致报错
    @XxlJob(value = "synDeviceMessageCensus")
    @GetMapping("/synDeviceMessageCensus")
    public void synDeviceMessageCensus() {
        log.info("供水设备信息同步任务启动成功");
        this.eqmsgreportNB();
        this.eqpicturereportNB();
        log.info("供水设备信息同步任务结束");
    }

}
